package com.hy.convenient.api.controller.down;

import com.hy.convenient.api.controller.conInterface.DownControllerInterface;
import com.hy.convenient.api.pojo.RegisterDownPojo;
import com.hy.convenient.api.pojo.RegisterModalCodePojo;
import com.hy.convenient.api.pojo.check.CheckUpdateCheckPojo;
import com.hy.convenient.api.pojo.check.RegisterCheckPojo;
import com.hy.convenient.api.server.RegisterDownServer;
import com.hy.convenient.api.server.RegisterModalCodeServer;
import com.hy.convenient.common.LayuiToTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 收银机模板参数下载
 * @className RegisterDateController
 * @Author hdy
 * @Date 2021-1-4 9:52
 * @Version 1.0
 */
@RestController
public class RegisterDateController  implements DownControllerInterface<RegisterCheckPojo> {
    @Autowired
    public RegisterDownServer registerDownServer;
    @Autowired
    public RegisterModalCodeServer registerModalCodeServer;

    /**
     * 下载所以收银机数据
     * @param t
     * @return
     */
    @PostMapping("/api/registerList")
    public Map<String, Object> downDataList(@RequestBody  RegisterCheckPojo t) {
//        if (!MD5.check(t.getMerId() + t.getCwbm()+t.getDateTime(), t.getSign())) {
//            return LayuiToTable.business_fail("签名失败");
//        }

        if (t.getPage() == 0) {
            t.setPage(1);
        }
        Map<String, Object> map = new HashMap();
        map.put("cwbm", t.getCwbm());
        map.put("syjh", t.getSyjh());
        map.put("merId", t.getMerId());
        map.put("pageSize", t.getPageSize());
        map.put("starSize", (t.getPage() - 1) * t.getPageSize());
        int count = registerDownServer.queryCount(map);
        List<RegisterDownPojo> list = registerDownServer.queryDownList(map);
        return LayuiToTable.layuiToTable(list, count);
    }

    /**
     * 下载某一个收银机数据及相关参数
     * @param t
     * @return
     */
    @Override
    @PostMapping("/api/register")
    public Map<String, Object> downData(@RequestBody  RegisterCheckPojo t) {
//        if (StringUtils.isEmpty(t.getSyjh())) {
//            return LayuiToTable.business_fail("收银机号不能为空");
//        }
//        String str = checkUpdate(t);
//        if (!StringUtils.isEmpty(str)) {
//            return LayuiToTable.business_fail(str);
//        }
//
//        if (!MD5.check(t.getMerId() + t.getCwbm()+t.getDateTime(), t.getSign())) {
//            return LayuiToTable.business_fail("签名失败");
//        }
        Map<String, Object> map = new HashMap();
        map.put("cwbm", t.getCwbm());
        map.put("syjh", t.getSyjh());
        map.put("merId", t.getMerId());
        map.put("pageSize",null);
        map.put("starSize", null);
        List<RegisterDownPojo> list = registerDownServer.queryDownList(map);
        map.clear();
        map.put("merId", t.getMerId());
        map.put("modalId", list.get(0).getModalId());
        map.put("pageSize", t.getPageSize());
        map.put("starSize", (t.getPage() - 1) * t.getPageSize());
        List<RegisterModalCodePojo> modalId = registerModalCodeServer.queryDownList(map);
        list.get(0).setModalCodePojos(modalId);
        return LayuiToTable.layuiToTable(list, list.size());
    }

    @Override
    public Map<String, Object> checkUpdateDateDown(CheckUpdateCheckPojo t) {
        return null;
    }
}
